package com.admin.action;

import com.admin.entity.Admin;
import com.admin.entity.Page;
import com.uni.entity.User;
import com.uni.service.Service;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author Uni
 * @create 2021/12/2 23:46
 */
@WebServlet(name = "SelectAllUserPageableAction", urlPatterns = "/adminSelectAllUserPageable")
public class SelectAllUserPageableAction extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        HttpSession session = req.getSession();
        String pageInfo = req.getParameter("page");
        if ( pageInfo == null) {
            pageInfo = "1";
        }
        int pageNow = Integer.parseInt(pageInfo);
        int pageCount = 6;

        List<User> users = Service.user.queryAllUser();

        int totalRows= users.size();
        int totalPages = (totalRows % pageCount == 0) ? (totalRows / pageCount) : (totalRows / pageCount + 1);

        if(pageNow < 1) pageNow = 1;
        if(pageNow > totalPages) pageNow = totalPages;
        Page page = new Page(pageNow, pageCount, totalPages, totalRows);
        session.setAttribute("adminUserPage", page);
        session.setAttribute("userList",users);
        resp.sendRedirect("card_admin_option_user.jsp");
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
